package jp.sourceforge.akjrcp.videotools.dvdtoxvid;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

public class DVDData {
private List titleSets=new ArrayList();
public void open(File dir){
	//log.info(dir.getAbsolutePath());
	titleSets=new ArrayList();
	String [] list=dir.list(new VTFFileFilter());
	for (int i = 0; i < list.length; i++) {
		int nums[]=getVTSNumber(list[i]);
		DVDTitleSet titleset=getTitleSet(nums[0]);
		if(titleset==null){
			 titleset=new DVDTitleSet(nums[0]);
			 titleSets.add(titleset);
		}
		titleset.addFile(new File(dir,list[i]));
	}
	
	if(titleSets.size()==0){
		
	if(dir.getAbsolutePath().length()==3){//maybe only root	
		if(new File(dir,"VIDEO_TS").exists()){
			open(new File(dir,"VIDEO_TS"));//special for root dir.
			}
		}
	}
}
public DVDTitleSet getTitleSet(int index){
	DVDTitleSet[] titles=getTitleSets();
	for (int i = 0; i < titles.length; i++) {
		if(titles[i].getIndex()==index){
			return titles[i];
		}
	}
	return null;
}
public static int[] getVTSNumber(String line){
	String base=line.substring(4,line.length()-4);//start VTS_ end .VOB
	String nums[]=base.split("_");
	int[] ret=new int[2];
	ret[0]=Integer.parseInt(nums[0]);
	ret[1]=Integer.parseInt(nums[1]);
	return ret;
}

public DVDTitleSet[] getTitleSets(){
	return (DVDTitleSet[]) titleSets.toArray(new DVDTitleSet[titleSets.size()]);
}

public class VTFFileFilter implements FilenameFilter{

	public boolean accept(File arg0, String arg1) {
		if(arg1.toUpperCase().endsWith(".VOB")){
			if(arg1.toUpperCase().startsWith("VTS_")){
				return true;
			}
		}
		return false;
	}
	
}
}
